using Microsoft.EntityFrameworkCore;
using Project_Schedule.Configures;
using Project_Schedule.Domain.BackGround.com.core.Interface;
using Project_Schedule.Domain.BackGround.com.core.utils;
using Project_Schedule.Domain.Request;
using Project_Schedule.Model;
using Project_Schedule.Model.EntitiesHelper;

namespace Project_Schedule.Domain.BackGround.com.core.Service;

public class Bg_AuditReceptionistService : IBackgroundAuditSchUser
{
    public async Task<JRequest> AuditSchUserStatus(int page)
    {
        try
        {
            await using var db = new DataDbContext();
            var info = db.SystemUsers?.Where(e =>
                    e.IsUsed == System_Audit_Status_Enum.None || e.IsUsed == System_Audit_Status_Enum.Refuse)
                .TakePage(page)
                .ToList();
            return JRequest.Ok(info);
        }
        catch (Exception exception)
        {
            _logHelper.LogError(exception);
            return JRequest.Ok(false, false, exception.Message);
        }
    }

    public async Task<JRequest> DelRefuseUser(string account)
    {
        try
        {
            await using var db = new DataDbContext();
            var info = await db.SystemUsers?.FirstOrDefaultAsync(
                e => e.Account == account);
            if (info is null) throw new Exception("查询此信息失败");
            if (info.IsUsed != System_Audit_Status_Enum.Refuse) throw new Exception("用户状态异常");
            db.SystemUsers.Remove(info);
            await db.SaveChangesAsync();

            return JRequest.Ok(true);
        }
        catch (Exception exception)
        {
            _logHelper.LogError(exception);
            return JRequest.Ok(false, false, exception.Message);
        }
    }

    private readonly INLogHelper _logHelper;

    public Bg_AuditReceptionistService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }
}